import React from 'react';
import { BrowserRouter as Router, Routes, Route, Navigate } from 'react-router-dom';
import routes from './routes/router';
import Login from './components/login';
function App() {


  const AuthGuard = ({ element, route }) => {
    // 检查是否需要验证
    const requiresAuth = route?.meta?.auth;
    let dl = true
    // 则重定向到登录页面
    if (requiresAuth) {
      if (dl) {
        return element;
      } else {
        return <Navigate to="/login" replace />;
      }
    } else {
      // 如果不需要验证或者已登录，则显示元素
      return element;
    }

  };

  return (
    <Router>
      <Routes>
        {routes.map((route, index) => (
          <Route
            key={index}
            path={route.path}
            element={<AuthGuard element={route.element} route={route} />}
          >
            {route.children?.map((childRoute, childIndex) => (
              <Route
                key={childIndex}
                path={childRoute.path}
                element={<AuthGuard element={childRoute.element} />}
              />
            ))}
          </Route>
        ))}
        {/* 确保有一个/login路由 */}
        <Route path="/login" element={<Login />} />
      </Routes>
    </Router>
  );
}

export default App;
